package withdraw

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/common"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawTypeListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// c端用户提现页面--提现配置--查询
func NewWithdrawTypeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawTypeListLogic {
	return &WithdrawTypeListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WithdrawTypeListLogic) WithdrawTypeList(req *types.WithdrawTypeListReq) (resp *types.WithdrawTypeListRes, err error) {
	UserId := headInfo.GetTokenUid(l.ctx)
	if UserId == 0 {
		return nil, xerrors.UserTokenInvalid
	}

	rpc_req := &rpc.GetWithDrawTypeRequest{
		Status: rpc.WithdrawStatus_Open,
		UserId: UserId,
	}

	// 发送请求
	rpc_resp, err := l.svcCtx.AssetsRpc.PayWithdraw.WithdrawTypeList(l.ctx, rpc_req)
	if err != nil {
		logx.Errorf("Call ModifyVipLevelInfos error: %v", err)
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.WithdrawTypeListRes{}
	for _, d := range rpc_resp.Data {
		if d.Status != int32(rpc.WithdrawStatus_Open) {
			continue
		}

		d2 := &types.WithdrawTypeListRes_Data{}
		copier.Copy(d2, d)
		if IsOpen(l.svcCtx, l.ctx, common.Valid, "status") == 0 {
			d2.Valid = 0
		} else {
			d2.Valid = rpc_resp.Valid
		}
		resp.Data = append(resp.Data, d2)

	}
	return
}
